﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace craigsPack
{
    public class CraigsListUrlBuilder : UrlBuilder
    {
        const string SALECAT = "sss";
        const string JOBSCAT = "jjj";
        const string HOUSECAT = "hhh";
        const string SERVICECAT = "bbb";
        const string GIGSCAT = "ggg";

        internal static string GetQueryStringCategory(CraigsListCategory category)
        {
            return category == CraigsListCategory.Gigs ? GIGSCAT :
                    category == CraigsListCategory.Housing ? HOUSECAT :
                    category == CraigsListCategory.Jobs ? JOBSCAT :
                    category == CraigsListCategory.Sale ? SALECAT :
                    category == CraigsListCategory.Services ? SERVICECAT : null;
        }

        public static CraigsListUrlBuilder Create(CraigsListCategory category)
        {
            string realCat = GetQueryStringCategory(category);

            CraigsListUrlBuilder b = new CraigsListUrlBuilder()
            {
                QueryBuilder = realCat == SALECAT ? CraigsListQueryStringBuilder.ForSaleQueryStringBuilder :
                realCat == JOBSCAT ? CraigsListQueryStringBuilder.JobsQueryStringBuilder :
                realCat == HOUSECAT ? CraigsListQueryStringBuilder.HousingQueryStringBuilder :
                realCat == SERVICECAT ? CraigsListQueryStringBuilder.ServicesQueryStringBuilder :
                realCat == GIGSCAT ? CraigsListQueryStringBuilder.GigsQueryStringBuilder : null
             
            };

           if (b.QueryBuilder == null)
               throw new ArgumentException("category not found");

           return b;
        }
        //private void SetCategory(string category)
        //{
        //    if (query.Contains('?'))
        //        throw new Exception("category already set");

        //    catIndex = query.Length - 1;
        //    catLen = category.Length - 1;
        //    query += category + '?';
        //}
        protected CraigsListUrlBuilder()  
            :base(null)
        {
            Domain = "craigslist.org";
            SubDomain = null;
        }
        public new string SubDomain
        {
            get { return base.SubDomain; }
            set { base.SubDomain = value; }
        }
        //public new CraigsListQueryStringBuilder QueryBuilder { get; protected set; }
    }
}
